package org.gap.service;

import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.gap.dao.AdherentDAO;
import org.gap.dao.CaractPMDAO;
import org.gap.dao.CaractPPDAO;
import org.gap.dao.PatientDAO;
import org.gap.dao.RelationsTiersDAO;
import org.gap.dao.RolesTiersDAO;
import org.gap.dao.TiersDAO;
import org.gap.dao.TiersModifDAO;
import org.gap.dao.UtilisateursDAO;

import org.gap.domain.Adherent;
import org.gap.domain.CaractPM;
import org.gap.domain.CaractPP;
import org.gap.domain.Patient;
import org.gap.domain.RelationsTiers;
import org.gap.domain.RolesTiers;
import org.gap.domain.Tiers;
import org.gap.domain.TiersModif;
import org.gap.domain.Utilisateurs;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Tiers entities
 * 
 */

@Service("TiersService")
@Transactional
public class TiersServiceImpl implements TiersService {

	/**
	 * DAO injected by Spring that manages Adherent entities
	 * 
	 */
	@Autowired
	private AdherentDAO adherentDAO;

	/**
	 * DAO injected by Spring that manages CaractPM entities
	 * 
	 */
	@Autowired
	private CaractPMDAO caractPMDAO;

	/**
	 * DAO injected by Spring that manages CaractPP entities
	 * 
	 */
	@Autowired
	private CaractPPDAO caractPPDAO;

	/**
	 * DAO injected by Spring that manages Patient entities
	 * 
	 */
	@Autowired
	private PatientDAO patientDAO;

	/**
	 * DAO injected by Spring that manages RelationsTiers entities
	 * 
	 */
	@Autowired
	private RelationsTiersDAO relationsTiersDAO;

	/**
	 * DAO injected by Spring that manages RolesTiers entities
	 * 
	 */
	@Autowired
	private RolesTiersDAO rolesTiersDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * DAO injected by Spring that manages TiersModif entities
	 * 
	 */
	@Autowired
	private TiersModifDAO tiersModifDAO;

	/**
	 * DAO injected by Spring that manages Utilisateurs entities
	 * 
	 */
	@Autowired
	private UtilisateursDAO utilisateursDAO;

	/**
	 * Instantiates a new TiersServiceImpl.
	 *
	 */
	public TiersServiceImpl() {
	}

	/**
	 * Load an existing Tiers entity
	 * 
	 */
	@Transactional
	public Set<Tiers> loadTierss() {
		return tiersDAO.findAllTierss();
	}

	/**
	 */
	@Transactional
	public Tiers findTiersByPrimaryKey(Integer tieTiers) {
		return tiersDAO.findTiersByPrimaryKey(tieTiers);
	}

	/**
	 * Save an existing Utilisateurs entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersUtilisateurses(Integer tieTiers, Utilisateurs related_utilisateurses) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		Utilisateurs existingutilisateurses = utilisateursDAO.findUtilisateursByPrimaryKey(related_utilisateurses.getUsrUser());

		// copy into the existing record to preserve existing relationships
		if (existingutilisateurses != null) {
			existingutilisateurses.setUsrUser(related_utilisateurses.getUsrUser());
			existingutilisateurses.setUsrPassword(related_utilisateurses.getUsrPassword());
			existingutilisateurses.setUsrService(related_utilisateurses.getUsrService());
			existingutilisateurses.setUsrMail(related_utilisateurses.getUsrMail());
			existingutilisateurses.setUsrEnabled(related_utilisateurses.getUsrEnabled());
			related_utilisateurses = existingutilisateurses;
		}

		related_utilisateurses.setTiers(tiers);
		tiers.getUtilisateurses().add(related_utilisateurses);
		related_utilisateurses = utilisateursDAO.store(related_utilisateurses);
		utilisateursDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Return a count of all Tiers entity
	 * 
	 */
	@Transactional
	public Integer countTierss() {
		return ((Long) tiersDAO.createQuerySingleResult("select count(o) from Tiers o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing CaractPM entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersCaractPM(Integer tieTiers, CaractPM related_caractpm) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		CaractPM existingcaractPM = caractPMDAO.findCaractPMByPrimaryKey(related_caractpm.getCpmTiers());

		// copy into the existing record to preserve existing relationships
		if (existingcaractPM != null) {
			existingcaractPM.setCpmTiers(related_caractpm.getCpmTiers());
			existingcaractPM.setCpmNomSociete(related_caractpm.getCpmNomSociete());
			existingcaractPM.setCpmFormeJuridique(related_caractpm.getCpmFormeJuridique());
			existingcaractPM.setCpmCodeNaf(related_caractpm.getCpmCodeNaf());
			existingcaractPM.setCpmSiren(related_caractpm.getCpmSiren());
			existingcaractPM.setCpmSiret(related_caractpm.getCpmSiret());
			existingcaractPM.setCpmTiersContact(related_caractpm.getCpmTiersContact());
			existingcaractPM.setCpmQualiteSociete(related_caractpm.getCpmQualiteSociete());
			existingcaractPM.setCpmEtatSociete(related_caractpm.getCpmEtatSociete());
			existingcaractPM.setCpmChiffreAffaire(related_caractpm.getCpmChiffreAffaire());
			existingcaractPM.setCpmNbrSalarier(related_caractpm.getCpmNbrSalarier());
			related_caractpm = existingcaractPM;
		} else {
			related_caractpm = caractPMDAO.store(related_caractpm);
			caractPMDAO.flush();
		}

		tiers.setCaractPM(related_caractpm);
		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		related_caractpm = caractPMDAO.store(related_caractpm);
		caractPMDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersRelationsTiersesForRltTiersFils(Integer tiers_tieTiers, Integer related_relationstiersesforrlttiersfils_rltTiersParent, Integer related_relationstiersesforrlttiersfils_rltTiersFils, String related_relationstiersesforrlttiersfils_rltRelation) {
		RelationsTiers related_relationstiersesforrlttiersfils = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstiersesforrlttiersfils_rltTiersParent, related_relationstiersesforrlttiersfils_rltTiersFils, related_relationstiersesforrlttiersfils_rltRelation, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_relationstiersesforrlttiersfils.setTiersByRltTiersFils(null);
		tiers.getRelationsTiersesForRltTiersFils().remove(related_relationstiersesforrlttiersfils);

		relationsTiersDAO.remove(related_relationstiersesforrlttiersfils);
		relationsTiersDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing CaractPP entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersCaractPP(Integer tieTiers, CaractPP related_caractpp) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		CaractPP existingcaractPP = caractPPDAO.findCaractPPByPrimaryKey(related_caractpp.getCppTiers());

		// copy into the existing record to preserve existing relationships
		if (existingcaractPP != null) {
			existingcaractPP.setCppTiers(related_caractpp.getCppTiers());
			existingcaractPP.setCppSexe(related_caractpp.getCppSexe());
			existingcaractPP.setCppQualite(related_caractpp.getCppQualite());
			existingcaractPP.setCppNomPers(related_caractpp.getCppNomPers());
			existingcaractPP.setCppPrenomPers(related_caractpp.getCppPrenomPers());
			existingcaractPP.setCppNomJeuneFille(related_caractpp.getCppNomJeuneFille());
			existingcaractPP.setCppDateNaissance(related_caractpp.getCppDateNaissance());
			existingcaractPP.setCppCsp(related_caractpp.getCppCsp());
			existingcaractPP.setCppRegimeSocial(related_caractpp.getCppRegimeSocial());
			existingcaractPP.setCppCodeProfession(related_caractpp.getCppCodeProfession());
			existingcaractPP.setCppSituationFamille(related_caractpp.getCppSituationFamille());
			existingcaractPP.setCppNombreEnfants(related_caractpp.getCppNombreEnfants());
			existingcaractPP.setCppNif(related_caractpp.getCppNif());
			existingcaractPP.setCppDateSituation(related_caractpp.getCppDateSituation());
			existingcaractPP.setCppDateEtatPhysique(related_caractpp.getCppDateEtatPhysique());
			existingcaractPP.setCppEtatPhysique(related_caractpp.getCppEtatPhysique());
			related_caractpp = existingcaractPP;
		} else {
			related_caractpp = caractPPDAO.store(related_caractpp);
			caractPPDAO.flush();
		}

		tiers.setCaractPP(related_caractpp);
		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		related_caractpp = caractPPDAO.store(related_caractpp);
		caractPPDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing TiersModif entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersTiersModifs(Integer tieTiers, TiersModif related_tiersmodifs) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		TiersModif existingtiersModifs = tiersModifDAO.findTiersModifByPrimaryKey(related_tiersmodifs.getTmoTiers(), related_tiersmodifs.getTmoNomChampModif(), related_tiersmodifs.getTmoDateModification());

		// copy into the existing record to preserve existing relationships
		if (existingtiersModifs != null) {
			existingtiersModifs.setTmoTiers(related_tiersmodifs.getTmoTiers());
			existingtiersModifs.setTmoNomChampModif(related_tiersmodifs.getTmoNomChampModif());
			existingtiersModifs.setTmoDateModification(related_tiersmodifs.getTmoDateModification());
			existingtiersModifs.setTmoAncienneVal(related_tiersmodifs.getTmoAncienneVal());
			existingtiersModifs.setTmoNouvelleVal(related_tiersmodifs.getTmoNouvelleVal());
			related_tiersmodifs = existingtiersModifs;
		}

		related_tiersmodifs.setTiers(tiers);
		tiers.getTiersModifs().add(related_tiersmodifs);
		related_tiersmodifs = tiersModifDAO.store(related_tiersmodifs);
		tiersModifDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing Adherent entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersAdherents(Integer tiers_tieTiers, Integer related_adherents_adhId) {
		Adherent related_adherents = adherentDAO.findAdherentByPrimaryKey(related_adherents_adhId, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_adherents.setTiers(null);
		tiers.getAdherents().remove(related_adherents);

		adherentDAO.remove(related_adherents);
		adherentDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersRelationsTiersesForRltTiersFils(Integer tieTiers, RelationsTiers related_relationstiersesforrlttiersfils) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		RelationsTiers existingrelationsTiersesForRltTiersFils = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstiersesforrlttiersfils.getRltTiersParent(), related_relationstiersesforrlttiersfils.getRltTiersFils(), related_relationstiersesforrlttiersfils.getRltRelation());

		// copy into the existing record to preserve existing relationships
		if (existingrelationsTiersesForRltTiersFils != null) {
			existingrelationsTiersesForRltTiersFils.setRltTiersParent(related_relationstiersesforrlttiersfils.getRltTiersParent());
			existingrelationsTiersesForRltTiersFils.setRltTiersFils(related_relationstiersesforrlttiersfils.getRltTiersFils());
			existingrelationsTiersesForRltTiersFils.setRltRelation(related_relationstiersesforrlttiersfils.getRltRelation());
			existingrelationsTiersesForRltTiersFils.setRltSens(related_relationstiersesforrlttiersfils.getRltSens());
			existingrelationsTiersesForRltTiersFils.setRltDateDebut(related_relationstiersesforrlttiersfils.getRltDateDebut());
			existingrelationsTiersesForRltTiersFils.setRltDateFin(related_relationstiersesforrlttiersfils.getRltDateFin());
			related_relationstiersesforrlttiersfils = existingrelationsTiersesForRltTiersFils;
		}

		related_relationstiersesforrlttiersfils.setTiersByRltTiersFils(tiers);
		tiers.getRelationsTiersesForRltTiersFils().add(related_relationstiersesforrlttiersfils);
		related_relationstiersesforrlttiersfils = relationsTiersDAO.store(related_relationstiersesforrlttiersfils);
		relationsTiersDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public void deleteTiers(Tiers tiers) {
		tiersDAO.remove(tiers);
		tiersDAO.flush();
	}

	/**
	 * Save an existing Patient entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersPatients(Integer tieTiers, Patient related_patients) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		Patient existingpatients = patientDAO.findPatientByPrimaryKey(related_patients.getPatPatient());

		// copy into the existing record to preserve existing relationships
		if (existingpatients != null) {
			existingpatients.setPatPatient(related_patients.getPatPatient());
			existingpatients.setPatDateEntree(related_patients.getPatDateEntree());
			existingpatients.setPatTelContact(related_patients.getPatTelContact());
			existingpatients.setPatDivers(related_patients.getPatDivers());
			existingpatients.setPatDateSortie(related_patients.getPatDateSortie());
			existingpatients.setPatAxeMedical(related_patients.getPatAxeMedical());
			existingpatients.setPatSousAxeMedical(related_patients.getPatSousAxeMedical());
			existingpatients.setPatMotifSortie(related_patients.getPatMotifSortie());
			existingpatients.setPatDiagnostic(related_patients.getPatDiagnostic());
			existingpatients.setPatDateDerniereVisite(related_patients.getPatDateDerniereVisite());
			related_patients = existingpatients;
		}

		related_patients.setTiers(tiers);
		tiers.getPatients().add(related_patients);
		related_patients = patientDAO.store(related_patients);
		patientDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Return all Tiers entity
	 * 
	 */
	@Transactional
	public List<Tiers> findAllTierss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Tiers>(tiersDAO.findAllTierss(startResult, maxRows));
	}

	/**
	 * Delete an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersRolesTierses(Integer tiers_tieTiers, String related_rolestierses_rotRole, Integer related_rolestierses_rotTiers) {
		RolesTiers related_rolestierses = rolesTiersDAO.findRolesTiersByPrimaryKey(related_rolestierses_rotRole, related_rolestierses_rotTiers, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_rolestierses.setTiers(null);
		tiers.getRolesTierses().remove(related_rolestierses);

		rolesTiersDAO.remove(related_rolestierses);
		rolesTiersDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersRelationsTiersesForRltTiersParent(Integer tiers_tieTiers, Integer related_relationstiersesforrlttiersparent_rltTiersParent, Integer related_relationstiersesforrlttiersparent_rltTiersFils, String related_relationstiersesforrlttiersparent_rltRelation) {
		RelationsTiers related_relationstiersesforrlttiersparent = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstiersesforrlttiersparent_rltTiersParent, related_relationstiersesforrlttiersparent_rltTiersFils, related_relationstiersesforrlttiersparent_rltRelation, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_relationstiersesforrlttiersparent.setTiersByRltTiersFils(null);
		tiers.getRelationsTiersesForRltTiersFils().remove(related_relationstiersesforrlttiersparent);

		relationsTiersDAO.remove(related_relationstiersesforrlttiersparent);
		relationsTiersDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersRelationsTiersesForRltTiersParent(Integer tieTiers, RelationsTiers related_relationstiersesforrlttiersparent) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		RelationsTiers existingrelationsTiersesForRltTiersParent = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstiersesforrlttiersparent.getRltTiersParent(), related_relationstiersesforrlttiersparent.getRltTiersFils(), related_relationstiersesforrlttiersparent.getRltRelation());

		// copy into the existing record to preserve existing relationships
		if (existingrelationsTiersesForRltTiersParent != null) {
			existingrelationsTiersesForRltTiersParent.setRltTiersParent(related_relationstiersesforrlttiersparent.getRltTiersParent());
			existingrelationsTiersesForRltTiersParent.setRltTiersFils(related_relationstiersesforrlttiersparent.getRltTiersFils());
			existingrelationsTiersesForRltTiersParent.setRltRelation(related_relationstiersesforrlttiersparent.getRltRelation());
			existingrelationsTiersesForRltTiersParent.setRltSens(related_relationstiersesforrlttiersparent.getRltSens());
			existingrelationsTiersesForRltTiersParent.setRltDateDebut(related_relationstiersesforrlttiersparent.getRltDateDebut());
			existingrelationsTiersesForRltTiersParent.setRltDateFin(related_relationstiersesforrlttiersparent.getRltDateFin());
			related_relationstiersesforrlttiersparent = existingrelationsTiersesForRltTiersParent;
		}

		related_relationstiersesforrlttiersparent.setTiersByRltTiersFils(tiers);
		tiers.getRelationsTiersesForRltTiersFils().add(related_relationstiersesforrlttiersparent);
		related_relationstiersesforrlttiersparent = relationsTiersDAO.store(related_relationstiersesforrlttiersparent);
		relationsTiersDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public void saveTiers(Tiers tiers) {
		Tiers existingTiers = tiersDAO.findTiersByPrimaryKey(tiers.getTieTiers());

		if (existingTiers != null) {
			if (existingTiers != tiers) {
				existingTiers.setTieTiers(tiers.getTieTiers());
				existingTiers.setTieIdentClient(tiers.getTieIdentClient());
				existingTiers.setTieTypeTiers(tiers.getTieTypeTiers());
				existingTiers.setTieLibExterne(tiers.getTieLibExterne());
				existingTiers.setTieNationalite(tiers.getTieNationalite());
				existingTiers.setTieTelFix(tiers.getTieTelFix());
				existingTiers.setTieTelBureau(tiers.getTieTelBureau());
				existingTiers.setTieTelPortable(tiers.getTieTelPortable());
				existingTiers.setTieFax(tiers.getTieFax());
				existingTiers.setTieEMail(tiers.getTieEMail());
				existingTiers.setTieDateCreation(tiers.getTieDateCreation());
				existingTiers.setTieUserCreation(tiers.getTieUserCreation());
			}
			tiers = tiersDAO.store(existingTiers);
		} else {
			tiers = tiersDAO.store(tiers);
		}
		tiersDAO.flush();
	}

	/**
	 * Delete an existing TiersModif entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersTiersModifs(Integer tiers_tieTiers, Integer related_tiersmodifs_tmoTiers, String related_tiersmodifs_tmoNomChampModif, Calendar related_tiersmodifs_tmoDateModification) {
		TiersModif related_tiersmodifs = tiersModifDAO.findTiersModifByPrimaryKey(related_tiersmodifs_tmoTiers, related_tiersmodifs_tmoNomChampModif, related_tiersmodifs_tmoDateModification, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_tiersmodifs.setTiers(null);
		tiers.getTiersModifs().remove(related_tiersmodifs);

		tiersModifDAO.remove(related_tiersmodifs);
		tiersModifDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing RolesTiers entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersRolesTierses(Integer tieTiers, RolesTiers related_rolestierses) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		RolesTiers existingrolesTierses = rolesTiersDAO.findRolesTiersByPrimaryKey(related_rolestierses.getRotRole(), related_rolestierses.getRotTiers());

		// copy into the existing record to preserve existing relationships
		if (existingrolesTierses != null) {
			existingrolesTierses.setRotRole(related_rolestierses.getRotRole());
			existingrolesTierses.setRotTiers(related_rolestierses.getRotTiers());
			existingrolesTierses.setRotDescriptif(related_rolestierses.getRotDescriptif());
			existingrolesTierses.setRotDateDebut(related_rolestierses.getRotDateDebut());
			existingrolesTierses.setRotDateFin(related_rolestierses.getRotDateFin());
			related_rolestierses = existingrolesTierses;
		}

		related_rolestierses.setTiers(tiers);
		tiers.getRolesTierses().add(related_rolestierses);
		related_rolestierses = rolesTiersDAO.store(related_rolestierses);
		rolesTiersDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing CaractPP entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersCaractPP(Integer tiers_tieTiers, Integer related_caractpp_cppTiers) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);
		CaractPP related_caractpp = caractPPDAO.findCaractPPByPrimaryKey(related_caractpp_cppTiers, -1, -1);

		tiers.setCaractPP(null);
		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		related_caractpp = caractPPDAO.store(related_caractpp);
		caractPPDAO.flush();

		caractPPDAO.remove(related_caractpp);
		caractPPDAO.flush();

		return tiers;
	}

	/**
	 * Save an existing Adherent entity
	 * 
	 */
	@Transactional
	public Tiers saveTiersAdherents(Integer tieTiers, Adherent related_adherents) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tieTiers, -1, -1);
		Adherent existingadherents = adherentDAO.findAdherentByPrimaryKey(related_adherents.getAdhId());

		// copy into the existing record to preserve existing relationships
		if (existingadherents != null) {
			existingadherents.setAdhId(related_adherents.getAdhId());
			existingadherents.setAdhCin(related_adherents.getAdhCin());
			existingadherents.setAdhImmatriculation(related_adherents.getAdhImmatriculation());
			existingadherents.setAdhAffiliation(related_adherents.getAdhAffiliation());
			existingadherents.setAdhDateAffiliation(related_adherents.getAdhDateAffiliation());
			existingadherents.setAdhPension(related_adherents.getAdhPension());
			existingadherents.setAdhSom(related_adherents.getAdhSom());
			related_adherents = existingadherents;
		}

		related_adherents.setTiers(tiers);
		tiers.getAdherents().add(related_adherents);
		related_adherents = adherentDAO.store(related_adherents);
		adherentDAO.flush();

		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing CaractPM entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersCaractPM(Integer tiers_tieTiers, Integer related_caractpm_cpmTiers) {
		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);
		CaractPM related_caractpm = caractPMDAO.findCaractPMByPrimaryKey(related_caractpm_cpmTiers, -1, -1);

		tiers.setCaractPM(null);
		tiers = tiersDAO.store(tiers);
		tiersDAO.flush();

		related_caractpm = caractPMDAO.store(related_caractpm);
		caractPMDAO.flush();

		caractPMDAO.remove(related_caractpm);
		caractPMDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing Utilisateurs entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersUtilisateurses(Integer tiers_tieTiers, String related_utilisateurses_usrUser) {
		Utilisateurs related_utilisateurses = utilisateursDAO.findUtilisateursByPrimaryKey(related_utilisateurses_usrUser, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_utilisateurses.setTiers(null);
		tiers.getUtilisateurses().remove(related_utilisateurses);

		utilisateursDAO.remove(related_utilisateurses);
		utilisateursDAO.flush();

		return tiers;
	}

	/**
	 * Delete an existing Patient entity
	 * 
	 */
	@Transactional
	public Tiers deleteTiersPatients(Integer tiers_tieTiers, Integer related_patients_patPatient) {
		Patient related_patients = patientDAO.findPatientByPrimaryKey(related_patients_patPatient, -1, -1);

		Tiers tiers = tiersDAO.findTiersByPrimaryKey(tiers_tieTiers, -1, -1);

		related_patients.setTiers(null);
		tiers.getPatients().remove(related_patients);

		patientDAO.remove(related_patients);
		patientDAO.flush();

		return tiers;
	}
}
